//index.js
//获取应用实例
const app = getApp();
var util = require('../../utils/util.js');

Page({
  data: {
    fapaiForm: {
      addtellHidden: false, //弹出框显示/隐藏
    },
  },
  onLoad: function () {
    let that = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('acceptDataFromOpenedPage', { data: "item" });
    eventChannel.emit('someEvent', { data: "item" });
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      console.log(data, "data")
      that.setData({
        id: data.id,
        load: data.load
      })
      that.getList()
      if (data.projectId != null) {
        that.setData({
          showProject: true
        })
        wx.setStorageSync('projectId', data.projectId)
      }
    })
  },
  // 点击图片
  getClickInfo: function (res) {
    console.log(res)
  },
  // 分享
  onShareAppMessage: function (res) {
    var that = this;
    return {
      title: '贷投狗小程序',
      path: 'pages/imge/imge',
      success: function (res) {
        that.shareClick();
      },
      fail: function (res) {
      }
    }
  },
  imgUrl: function () {
    var imgList = [];
    this.data.content.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/g, (match, capture) => {
      imgList.push(capture);
    });
    this.setData({
      imgSrc: imgList
    })
    return imgList;
  },
  handleLongPress: function (e) {
    this.imgUrl()
    if (this.data.imgSrc.length > 0) {
      wx.previewImage({
        urls: this.data.imgSrc // 需要预览的图片http链接列表
      })
    }
  },
  getList: function () {
    let that = this
    let id = this.data.id
    util.https(app.globalData.api + "/applet/column/findContent/" + id, "GET", {},
      function (data) {
        if (data.code == 200) {
          if (data.data) {
            that.setData({
              content1: data.data,
              imgData: data.data.content,
              showAccessory: data.data.showAccessory,
              singleUrl: data.data.singleUrl
            })
            if (that.data.singleUrl == null) {
              that.setData({
                load: false
              })
            }
            var content = that.data.content1.content;
            let newContent = content.replace(/(\<img|\<p)/gi, function ($0, $1) {
              return {
                "<img": '<img style="width:100%;height:auto;display:block;" ',
                "<p": '<p style="text-indent: 24px;" ',
                "<article": "<div",
                "</article": "</div",
                "<header": "<div",
                "</header": "</div"
              }[$1];
            });
            that.setData({
              content: newContent,

            })
          } else {
            that.setData({
              content: data.data
            })
          }
          if (that.data.content) {
            that.setData({
              show: false
            })
          } else {
            that.setData({
              show: true
            })
          }
        } else {
          util.toolTip(that, data.message)
        }
      }
    )
  },
  news: function (event) {
    // wx.setStorageSync('productId', event.currentTarget.dataset.id)
    // wx.setStorageSync('fapaiId', event.currentTarget.dataset.id)
    // wx.navigateTo({
    //   url: '/pages/order/order',
    // })
    this.setData({
      productId: event.currentTarget.dataset.id
    })
    let that = this;
    that.setData({
      fapaiForm: {
        addtellHidden: true, //弹出框显示/隐藏
      },
    })
  },
  formSubmit: function (e) {
    console.log(e, "00000000")
    let formData = e.detail.value;
    formData.productId = this.data.productId;
    let card = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/
    if (formData.basicBorrowerCard && formData.basicBorrowerName && formData.basicBorrowerPhone && formData.basicLoansMoney && formData.pledgeHouseAddress) {
      if (!card.test(formData.basicBorrowerCard)) {
        wx.showModal({
          title: '温馨提示',
          content: '请填写正确的申请人身份证号',
          showCancel: false,
          success(res) {
            if (res.confirm) { }
          }
        })
        return false
      }
      if (!/^1[3456789]\d{9}$/.test(formData.basicBorrowerPhone)) {
        wx.showModal({
          title: '温馨提示',
          content: '请填写正确的申请人联系电话',
          showCancel: false,
          success(res) {
            if (res.confirm) { }
          }
        })
        return false
      }
      this.submit(formData)
    } else {
      console.log("成功")
      wx.showModal({
        title: '温馨提示',
        content: '请完成信息录入',
        showCancel: false,
        success(res) {
          if (res.confirm) {

          }
        }
      })
    }
  },
  submit: function (formData) {
    let thisObj = this;
    util.https(app.globalData.api + "/applet/order/submit", "POST", formData,
        function (data) {
            console.log(data, '00')
            if (data.code == 200) {
                thisObj.setData({
                    fapaiForm:{
                      addtellHidden: false,
                    }
                })
                thisObj.getList()
                wx.pageScrollTo({
                    scrollTop: 0
                })
            } else {
                wx.showModal({
                    title: '温馨提示',
                    content: data.message,
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {

                        }
                    }
                })
            }
        }
    )
},
  // 关闭弹框
  modalCancel: function () {
    //弹出框取消操作
    this.setData({
      fapaiForm: {
        addtellHidden: false,
      }
    })
  },
  // 下载附件
  load: function () {
    console.log(this.data.singleUrl)
    wx.downloadFile({
      url: this.data.singleUrl,
      success: function (res) {
        console.log(res);
        var rr = res.tempFilePath;
        wx.saveImageToPhotosAlbum({
          filePath: rr,
          success(res) {
            wx.showToast({
              title: '保存成功',
              icon: 'success',
            })
          }
        })
      }
    })
  }
})